package com.maserb.monitor.entity.schoolStudent;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.maserb.monitor.ApplicationUtils;
import com.maserb.monitor.entity.BaseIdEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolConduct.StudentConductRecordEntity;
import com.maserb.monitor.service.common.FileService;
import lombok.Data;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.io.File;
import java.text.DecimalFormat;
import java.util.List;

//学生信息表
@Data
@Entity
@Table(name = "school_student")
public class SchoolStudentEntity extends BaseIdEntity implements java.io.Serializable {
    //外部对接的学生id
    private Long cid;
    //学号
    @Excel(name = "学号", width = 25, orderNum = "1")
    private String studentId;
    //学生姓名
    @Excel(name = "学生姓名", width = 25, orderNum = "2")
    private String name;
    //根节点（所属学校）
    private String schoolId;
    //根节点（所属学校）
    private String schoolName;
    //班级ID
    private String orgId;
    //班级名称
    private String orgName;
    //外部对接的部门id
    private Long did;
    //手机号码
    @Excel(name = "手机号码", width = 25, orderNum = "3")
    private String phoneNumber;
    //头像
    private String image;
    //证件号码
    @Excel(name = "证件号码", width = 25, orderNum = "4")
    private String idcard;
    /**
     * 性别 1：男 2：女
     */
    @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "5", width = 20)
    private String sex;
    //生日
    @Excel(name = "生日", width = 25, orderNum = "6")
    private String birthday;
    //性别
    private String sexCn;
    @Excel(name = "描述", width = 25, orderNum = "6")
    private String description;
    private String isDel;
    private String createUser;
    private String createTime;
    private String updateUser;
    private String updateTime;
    //操行得分
    private Integer conductScore;

    //本学年考试平均分
    private Double examScore;

    //评价得分
    private Double evaluationScore;

    //家长
    private SchoolStudentParentEntity parent;

    //班级班主任
    private UserEntity master;

    /**
     * 卡号  只保存最新的卡号
     */
    private String cardNumber;

    /**
     * 操行
     */
    private List<StudentConductRecordEntity> conductRecordList;

    @Column(name = "student_id", length = 50)
    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @Column(name = "name", length = 50)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "school_id", length = 36)
    public String getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(String schoolId) {
        this.schoolId = schoolId;
    }

    @Column(name = "student_name", length = 50)
    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    @Column(name = "clazz_id", length = 36)
    public String getOrgId() {
        return orgId;
    }

    public void setOrgId(String orgId) {
        this.orgId = orgId;
    }

    @Column(name = "clazz_name", length = 50)
    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    @Column(name = "phone_number", length = 20)
    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @Column(name = "image")
    public String getImage() {
        String imageUrl = getSchoolId() + "\\" + getStudentId() + ".jpg";
        String filePath = getFileService().getUploadDir() + getSchoolId() + File.separator + getStudentId() + ".jpg";
        File tmp = new File(filePath);
        if (tmp.exists()) {
            imageUrl = imageUrl.replaceAll("\\\\", "/");
            return imageUrl;
        }

        return null;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Column(name = "idcard", length = 20)
    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    @Column(name = "sex", length = 5)
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Column(name = "birthday", length = 20)
    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Transient
    public String getSexCn() {
        return sexCn;
    }

    public void setSexCn(String sexCn) {
        this.sexCn = sexCn;
    }

    @Column(name = "description", length = 500)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "is_del", length = 5)
    public String getIsDel() {
        return isDel;
    }

    public void setIsDel(String isDel) {
        this.isDel = isDel;
    }

    @Column(name = "create_user", length = 50)
    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    @Column(name = "create_time", length = 20)
    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    @Column(name = "update_user", length = 50)
    public String getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }

    @Column(name = "update_time", length = 20)
    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    @NotFound(action = NotFoundAction.IGNORE)
    public SchoolStudentParentEntity getParent() {
        return parent;
    }

    public void setParent(SchoolStudentParentEntity parent) {
        this.parent = parent;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "master_id")
    @NotFound(action = NotFoundAction.IGNORE)
    public UserEntity getMaster() {
        return master;
    }

    public void setMaster(UserEntity master) {
        this.master = master;
    }

    @Column(name = "conduct_score")
    public Integer getConductScore() {
        return conductScore;
    }

    public void setConductScore(Integer conductScore) {
        this.conductScore = conductScore;
    }

    @Column(name = "card_number", length = 50)
    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    @Transient
    public List<StudentConductRecordEntity> getConductRecordList() {
        return conductRecordList;
    }

    public void setConductRecordList(List<StudentConductRecordEntity> conductRecordList) {
        this.conductRecordList = conductRecordList;
    }

    @Column(name = "cid")
    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    @Column(name = "did")
    public Long getDid() {
        return did;
    }

    public void setDid(Long did) {
        this.did = did;
    }

    @Transient
    public Double getExamScore() {
        return examScore;
    }

    public void setExamScore(Double examScore) {
        this.examScore = examScore;
    }

    @Transient
    public Double getEvaluationScore() {
        return evaluationScore;
    }

    public void setEvaluationScore(Double evaluationScore) {
        this.evaluationScore = evaluationScore;
    }

    @Transient
    public String getFormatEvaluationScore() {
        DecimalFormat df = new DecimalFormat("0.0");
        if (evaluationScore == null) {
            return "0";
        }

        return df.format(evaluationScore);
    }

    @Transient
    public String getFormatExamScore() {
        DecimalFormat df = new DecimalFormat("0.0");
        if (examScore == null) {
            return "0";
        }

        return df.format(examScore);
    }

    static FileService getFileService() {
        return ApplicationUtils.getBean(FileService.class);
    }
}
